package offer;

import java.util.HashMap;
import java.util.Map;

public class offer48 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "abba";
        solution.lengthOfLongestSubstring(s);
    }

    static class Solution {
        public int lengthOfLongestSubstring(String s) {
            char[] arr = s.toCharArray();
            int len = arr.length;
            int ans = 0;
            int i = 0, j = 0;
            Map<Character,Integer> map = new HashMap<>();
            while(i <= j){
                if(j < len){
                    if(map.containsKey(arr[j])){
                        int index = map.get(arr[j]);
                        if(index >= i && index <=j){
                            i = index + 1;
                        }
                        map.put(arr[j],j);
                    } else {
                        map.put(arr[j],j);
                    }
                    j++;
                    ans = Math.max(ans,(j-i));
                } else {
                    break;
                }
            }
            return ans;
        }
    }
}
